package com.fzrskj.cloud.free.sys.utils;

import com.fzrskj.cloud.free.core.enums.DicParamEnum;
import com.fzrskj.cloud.free.core.utils.HtmlUtils;
import com.fzrskj.cloud.free.dic.utils.DicParamUtils;
import com.gargoylesoftware.htmlunit.HttpMethod;
import org.apache.commons.collections.map.HashedMap;
import org.apache.http.HttpStatus;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

/**
 * AgentRequestUtils.
 *
 * @author zj.
 *         Created on 2019/5/10 0010.
 */
public class AgentRequestUtils {
    public static Map<String, String> map = new HashedMap();
    private final static String newsList = "news/list.html";
    private final static String newsInfoLink = "news/info.html";
    private final static String newsInfoLinkOld = "news/newsinfo.html";
    private final static String supportList = "support/list.html";
    private final static String supportInfoLink = "support/info.html";
    private final static String supportInfoLinkOld = "support/detail.html";
    static {
        map.put(newsList, "news/news.html");
        map.put(newsInfoLink, newsInfoLinkOld);
        map.put(supportList, "support/support.html");
        map.put(supportInfoLink, supportInfoLinkOld);
    }

    /**
     * String tmpDir = DicParamUtils.getParamValue(DicParamEnum.TEMPLATE_KEY.getVal());
     String indexUri = tmpDir + "index";

     if (indexUri.equals(uri)) {
     String html = HtmlUtils.agentHtml("http://localhost:888/default/index.html");
     resp.setHeader("Content-Type", "text/html");
     resp.setStatus(HttpStatus.SC_OK);
     resp.getOutputStream().write(html.getBytes("UTF-8"));
     return;
     }
     * @param request
     * @param response
     * @param uri
     * @return
     */

    public static Boolean agentRequest(HttpServletRequest request, HttpServletResponse response, String uri) {
        if (!request.getMethod().equals(HttpMethod.GET.name())) {
            return false;
        }
        String tmpDir = DicParamUtils.getParamValue(DicParamEnum.TEMPLATE_KEY.getVal());
        uri = uri.replaceAll(tmpDir, "");
        if (!map.containsKey(uri)) {
            return false;
        }

        String fullUrl = request.getRequestURL() + "?" + request.getQueryString();
        fullUrl = fullUrl.replace(uri, map.get(uri));

        String html = HtmlUtils.agentHtml(fullUrl);
        html = publicReplace(html, uri);

        response.setHeader("Content-Type", "text/html");
        response.setStatus(HttpStatus.SC_OK);
        try {
            response.getOutputStream().write(html.getBytes("UTF-8"));
        } catch (IOException e) {
            e.printStackTrace();
        }
        return true;
    }

    /**
     * 公共错误路径替换.
     * @param html html.
     * @return html.
     */
    private static String publicReplace(String html, String uri) {
        html = html.replaceAll("public/js/extend/layer.ext.js", "public/layer/extend/layer.ext.js");
        html = html.replaceAll("public/js/need/laydate.css", "public/laydate/need/laydate.css");
        html = html.replaceAll("public/js/skins/default/laydate.css", "public/laydate/skins/default/laydate.css");
        html = html.replaceAll("public/js/skin/layer.css", "public/layer/skin/layer.css");
        html = html.replaceAll("public/js/skin/layer.ext.css", "public/layer/skin/layer.ext.css");
        if (newsList.equals(uri)) {
            html = html.replaceAll("newsinfo.html", "info.html");
        }
        if (supportList.equals(uri)) {
            html = html.replaceAll("detail.html", "info.html");
        }
        return html;
    }
}
